
 /**
 * AsyncJavaMailSender.java.java.
 * User: BrianChen Date: 2012-6-20 下午10:23:36
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.email;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.util.Assert;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-6-20 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-6-20 下午10:23:36
 * @version 1.0
 * @see
 * 
 * 使用线程池异步发送邮件的javaMailSender
 * 每一个发送方法返回AsyncToken用于监听邮件是否发送成功
 */
public class AsyncJavaMailSender implements InitializingBean, DisposableBean, BeanNameAware{
	private static Logger logger = LoggerFactory.getLogger(AsyncJavaMailSender.class);
	
	private ExecutorService executorService = null;
	private int sendMailThreadPoolSize = 0;
	private boolean shutdownExecutorService = true;
	private boolean shutdownExecutorServiceWithTasksCompleted = true;
	private JavaMailSender javaMailSender = null;
	
	private String beanName = null;
	
	@Override
	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub 
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		if(this.executorService == null && this.sendMailThreadPoolSize > 0){
			this.executorService = Executors.newFixedThreadPool(this.sendMailThreadPoolSize, new CustomizableThreadFactory(getClass().getSimpleName()+"-"));
			logger.info("create send mail executorService,sendMailThreadPoolSize:" + this.sendMailThreadPoolSize);
		} 
		Assert.notNull(this.javaMailSender, "javaMailSender must be not null");
		Assert.notNull(this.executorService, "executorService must be not null");
		
	}
	
	
}

